[VB]单击加1的问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 08:38:12
单击加1,下面是代码.
Public Class Form1
Dim i As Integer
Dim n As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i = n To 50
i = i + 1
n = i
Label1.Text = i
Exit For
Next
End Sub
End Class

我想问的是为什么要用到2个变量,而用一个变量就不行了呢?比如:
Dim i As Integer
-------------------------
循环部分为:
For i = 0 To 50
i = i + 1
Label1.Text = i
Exit For
Next

你这里混淆了概念,变量n是作为循环变量用的.变量i是在赋值式中使用.
然而你需实现的单击Button1加1是不需FOR NEXT循环的.
Public Class Form1
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
i = i + 1
Label1.Text = i
End Sub
End Class